<#macro edit operate>
	<#if !(operate?? && operate == 5) >
	<#nested>
	</#if>
</#macro>

<#macro dictOption code key="">
	<#assign key_str = "" />
	<#if key??>
		<#if key?is_string >
			<#assign key_str = key />
		<#else>
			<#assign key_str = key?c />
		</#if>
	</#if>
		
	<#list CommonData.getDictList(code) as dict >
		<#if key_str?? && key_str == dict.dictKey >
			<option value="${dict.dictKey}" selected="selected">${dict.dictValue}</option>
		<#else>
			<option value="${dict.dictKey}">${dict.dictValue}</option>
		</#if>
	</#list>
</#macro>

<#macro dictValue code key="">
	<#if key?is_string >
		<#if key != "">
			${(CommonData.getDictValue(code, key))!}
		</#if>
	<#else>
		${(CommonData.getDictValue(code, key?c))!}
	</#if>
</#macro>

<#macro dictSetting code selectBoxId="">
	<div id="dict-setting-${code}">
		<#nested>
	</div>
	
	<script type="text/javascript">
	$(function () {
		$("#dict-setting-${code} .dict-operate").click(function(event) {
			var layer = window.layer;
			layer.open({
                type: 2,
                title: "字典设置",
                closeBtn: 1,
                maxmin: true,
                area: ['94%', '94%'],
                content: ["/admin/sys/dict/value/list?dictCode=${code}", "yes"],
                end:function (){
                    <#if selectBoxId?? && selectBoxId != ''>
	                    ajax.getData("/admin/sys/dict/value/ajaxList?dictCode=${code}", {}, function(data) {
	                    	var selectBox = "${selectBoxId}".startsWith(".") ? $("${selectBoxId}") : $("#${selectBoxId}");
	                        selectBox.each(function() {
	                        	var defaultKey = $(this).val();
	                        	
	                        	var str = "";
		            			for (var i=0; i<data.length; i++) {
		            				str += '<option value="'+data[i].dictKey+'">'+data[i].dictValue+'</option>';
		            			}
		            			
		            			$(this).find("option[value!='']").remove();
		            			$(this).append(str);
		            			if (defaultKey) {
		            				$(this).find("option[value='"+defaultKey+"']").attr("selected", true);
		            			}
	                        });
	            		});
                    </#if>
                }

            });
	    });
	});
	</script>
</#macro>

<#macro dataOption url boxId="data-select-box">
	<div id="${boxId}">
		<#nested>
	</div>
	
	<script type="text/javascript">
	$(function () {
		htm.ajaxOption($("#${boxId} select"), "${url}");
	});
	</script>
</#macro>

<#macro dictRadio name code key="">
	<#assign key_str = "" />
	<#if key?? >
		<#if key?is_string >
			<#assign key_str = key />
		<#else>
			<#assign key_str = key?c />
		</#if>
	</#if>
		
	<#list CommonData.getDictList(code) as dict >
		<#if key_str?? && key_str == dict.dictKey >
			<label class="chkLab control-value"><input type="radio" name="${name}" value="${dict.dictKey}" checked="checked" />${dict.dictValue}</label>
		<#else>
			<label class="chkLab control-value"><input type="radio" name="${name}" value="${dict.dictKey}" />${dict.dictValue}</label>
		</#if>
	</#list>
</#macro>

<#macro radioFlag name flag="" yesLabel="是" noLabel="否" >
	<#if !flag?? || flag?is_string >
		<label class="chkLab control-value"><input type="radio" name="${name}" value="1" />${yesLabel}</label>
    	<label class="chkLab control-value"><input type="radio" name="${name}" value="0" />${noLabel}</label>
    <#else>
    	<#if flag?is_boolean >
    		<#assign flag_bool = flag />
    	<#elseif flag?is_number > 
    		<#if flag = 1> <#assign flag_bool = true /> <#else> <#assign flag_bool = false /> </#if>
    	</#if>
	</#if>
	
	<#if flag_bool??>
		<#if flag_bool>
			<label class="chkLab control-value"><input type="radio" name="${name}" value="1" checked="checked" />${yesLabel}</label>
	    	<label class="chkLab control-value"><input type="radio" name="${name}" value="0" />${noLabel}</label>
	    <#else>
	    	<label class="chkLab control-value"><input type="radio" name="${name}" value="1" />${yesLabel}</label>
	    	<label class="chkLab control-value"><input type="radio" name="${name}" value="0" checked="checked" />${noLabel}</label>
	    </#if> 
	</#if>
</#macro>

<#macro radioLabel name flag="" yesLabel="是" noLabel="否" >
	<#if !(!flag?? || flag?is_string) >
    	<#if flag?is_boolean >
    		<#assign flag_bool = flag />
    	<#elseif flag?is_number > 
    		<#if flag = 1> <#assign flag_bool = true /> <#else> <#assign flag_bool = false /> </#if>
    	</#if>
	</#if>
	
	<#if flag_bool??>
		<#if flag_bool>
			${yesLabel}
	    <#else>
	    	${noLabel}
	    </#if> 
	</#if>
</#macro>

<#macro radioCheck name value="" data={} >
	<#assign value_str = "" />
	<#if value??>
		<#if value?is_string >
			<#assign value_str = value />
		<#else>
			<#assign value_str = value?c />
		</#if>
	</#if>
	
	<#list data?keys as key>
		<#assign key_str = "" />
		<#if key?is_string >
			<#assign key_str = key />
		<#else>
			<#assign key_str = key?c />
		</#if>
	
	    <#if key_str = value_str>
			<label class="chkLab control-value"><input type="radio" name="${name}" value="${key}" checked="checked" />${data[key]}</label>
	    <#else>
	    	<label class="chkLab control-value"><input type="radio" name="${name}" value="${key}" />${data[key]}</label>
	    </#if> 
	</#list>
</#macro>

<#macro radioCheckedData name value="" data={} >
	<#assign value_str = "" />
	<#if value??>
		<#if value?is_string >
			<#assign value_str = value />
		<#else>
			<#assign value_str = value?c />
		</#if>
	</#if>
	
	<#list data?keys as key>
		<#assign key_str = "" />
		<#if key?is_string >
			<#assign key_str = key />
		<#else>
			<#assign key_str = key?c />
		</#if>
	
	    <#if key_str = value_str>
			${data[key]}</#if> 
	</#list>
</#macro>

<#macro operateUser userId=0 >
<#if userId?? && userId != 0>${(AdminUtils.getSysUserName(userId))!}</#if>
</#macro>

<#macro currentUser>
	${Session.SESSION_USER_INFO.loginName}
</#macro>

<#macro userSelect boxId="user-select-box" fn="">
	<div id="${boxId}">
		<#nested>
	</div>
	
	<script type="text/javascript">
	<#if !fn?? || fn = ''>
		<#assign fn_=boxId?replace("-","_") + "_callback" />
		var ${fn_} = function(data) {
			$("#${boxId} .user-name").val(data.userName);
			$("#${boxId} .user-id").val(data.userId);
		}
	<#else>
	<#assign fn_=fn />
	</#if>
	
	$(function () {
		$("#${boxId} .user-operate").click(function(event) {
			var layer = window.layer;
			layer.open({
                type: 2,
                title: "选择人员",
                closeBtn: 1,
                maxmin: true,
                area: ['800px', '600px'],
                content: ["/admin/sys/user/select?fn=${fn_}&selectType=1", "yes"],
                end:function (){
                }
            });
	    });
	});
	
	</script>
</#macro>

<#macro userBatchSelect boxId="user-batch-select-box" fn="">
	<div id="${boxId}">
		<#nested>
	</div>
	
	<script type="text/javascript">
	<#if !fn?? || fn = ''>
		<#assign fn_=boxId?replace("-","_") + "_callback" />
		var ${fn_} = function(datas) {
			
			
			$("#${boxId} .user-name").val(datas.map(function(i){return i.userName}).join("，"));
			$("#${boxId} .user-id").val(datas.map(function(i){return i.userId}).join(","));
		}
	<#else>
	<#assign fn_=fn />
	</#if>
	
	$(function () {
		$("#${boxId} .user-operate").click(function(event) {
			var layer = window.layer;
			var selectedUserIds = $("#${boxId} .user-id").val();
			layer.open({
                type: 2,
                title: "选择人员",
                closeBtn: 1,
                maxmin: true,
                area: ['800px', '600px'],
                content: ["/admin/sys/user/select?fn=${fn_}&selectType=2&selectedUserIds=" + selectedUserIds, "yes"],
                end:function (){
                }
            });
	    });
	});
	
	</script>
</#macro>

<#macro cityPicker boxId="areaBoxId">
	<div id="${boxId}" class="pos">
		<#nested>
	</div>
	
	<script type="text/javascript">
	$(function () {
		$("#${boxId} .area-operate").citypicker();
		
// 		$("#${boxId} .city-picker-dropdown a").click(function() {
// 			alert($("#${boxId} .area-operate").data('citypicker').getVal(""));
// 			$("#${boxId} .area-val").val($("#${boxId} .area-operate").data('citypicker').getVal(""));
// 		});

	});
	</script>
</#macro>

<#macro batchInput boxId="batch-input-box">
	<div id="${boxId}">
		<#nested>
		
		<div id="${boxId}-dialog" class="main-container form-inline" style="display: none; height : 100%">
			<textarea cols="" class="form-control ${boxId}-textarea" style="height : 100%; width: 550px!important"></textarea>
		</div>
	</div>
	
	<script type="text/javascript">
	$(function () {
		var val = $("#${boxId} .batch-input-val").val();
		if (val != "") {
			$("#${boxId} .batch-input-operate").val("已选择" + val.split(",").length + "条数据");
		}
		
		$("#${boxId} .batch-input-operate").click(function(event) {
			var val = $("#${boxId} .batch-input-val").val();
			$("#${boxId} .${boxId}-textarea").val(val.replaceAll(",", "\n"));
			
			var layer = window.layer;
			layer.open({
				type: 1,
	            title: "多行输入查询",
	            area: '1300px',
	            btnAlign: 'c',
	            area : ['600px', '80%'],
	            closeBtn: 0,
	            content: $('#${boxId}-dialog'),
	            btn: ['确认','取消'],
	            yes: function (index, layero) {
	            	var val = $("#${boxId} .${boxId}-textarea").val().trim();
	            	$("#${boxId} .batch-input-val").val(val.replaceAll("\n", ","));
	            	if (val != "") {
	            		$("#${boxId} .batch-input-operate").val("已选择"+val.split("\n").length+"条数据");
	            	} else {
	            		$("#${boxId} .batch-input-operate").val("");
	            	}
	            	layer.close(index);
	            }
            });
	    });
	});
	</script>
</#macro>

<#macro currentPageTotal>
	<tr id="currentPageTotal" style="color: red;font-weight: bolder"></tr>
	<script type="text/javascript">
	$(function () {	
		var currentPageTable = $("#currentPageTotal").parents(".table");
		if (currentPageTable.find(".no-data").length > 0 || currentPageTable.find("tbody tr").length == 0) {
			$("#currentPageTotal").remove();
		} else {
			currentPageTable.find("thead th").each(function(index){
				if (index == 0) {
					$("#currentPageTotal").append("<td style='min-width: 120px;'>【当前页合计】</td>");
					return;
				}
				if (!$(this).hasClass("total-column")) {
					$("#currentPageTotal").append("<td></td>");
					return;
				}
				
				var total = 0;
				currentPageTable.find("tbody tr").each(function() {
					var val = $(this).find("td:eq(" + index + ")").text().replaceAll(",", "");
					if (val) {
						total = math.add(total, val);
					}
				});
				$("#currentPageTotal").append("<td>" + total + "</td>");
			});
		}
	});
	</script>
</#macro>

<#macro allPageTotal pageTotalData>
	<tr id="allPageTotal" style="color: red;font-weight: bolder"></tr>
	<script type="text/javascript">
	$(function () {	
		var allPageTable = $("#allPageTotal").parents(".table");
		if (allPageTable.find(".no-data").length > 0 || allPageTable.find("tbody tr").length == 0) {
			$("#allPageTotal").remove();
		} else {
			var pageTotalData = ${pageTotalData};
			allPageTable.find("thead th").each(function(index){
				if (index == 0) {
					$("#allPageTotal").append("<td style='min-width: 120px;'>【所有页合计】</td>");
					return;
				}
				if (!$(this).hasClass("total-column")) {
					$("#allPageTotal").append("<td></td>");
					return;
				}
				
				if (pageTotalData[$(this).attr("data-total")]) {
					$("#allPageTotal").append("<td>" + pageTotalData[$(this).attr("data-total")] + "</td>");
				} else {
					$("#allPageTotal").append("<td>0</td>");
				}
			});
		}
	});
	</script>
</#macro>

<#macro pageSortTable>
	<#nested>
	<script type="text/javascript">
	$(function () {
		$("th.sort").each(function() {
			if (!$(this).attr("data-sort")) {
				$(this).removeClass("sort");
			}
			if ($("#pageOrderBy").val() != "") {
				var pageOrderBy = $("#pageOrderBy").val();
				var sort = pageOrderBy.indexOf("desc") > 0 ? "down" : "up";
				if ($(this).attr("data-sort") == pageOrderBy.replaceAll(" desc", "")) {
					$(this).addClass(sort);
				}
			}
		});
		
		$("th.sort, th.sorted").click(function() {
			if ($(this).hasClass("down")) {
				$(this).removeClass("down").addClass("up");
				$("#pageOrderBy").val($(this).attr("data-sort"));
			} else {
				$(this).removeClass("up").addClass("down");
				$("#pageOrderBy").val($(this).attr("data-sort") + " desc");
			}
			$(this).siblings("th").removeClass("up").removeClass("down");
			page.search();
		});
		
	});
	</script>
</#macro>
